package com.simplenix.nicasio.admin.requestfilters;

import com.simplenix.nicasio.mb.Session;
import com.simplenix.nicasio.sys.RequestFilter;
import com.simplenix.nicasio.sys.SystemDef;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author fronald
 */
public class AdminFilter implements RequestFilter {

	public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
		if (request.getSession().getAttribute(Session.CURRENT_USER) == null) {
			try {
				String requestUri = request.getRequestURI();
				if ((requestUri.endsWith(".jsp") || !requestUri.contains(".")) && !requestUri.equals(SystemDef.getInstance().getContextPath() + "/") && !requestUri.equals(SystemDef.getInstance().getContextPath() + "/admin/misc/login.htm") && !requestUri.equals(SystemDef.getInstance().getContextPath() + "/index.jsp")) {
					response.sendRedirect(SystemDef.getInstance().getContextPath() + "/admin/misc/login.htm");
				}
			} catch (Exception ex) {
				Logger.getLogger(AdminFilter.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	}

}
